home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / fl_show_colormap.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  4.0 KB  |  152 lines

  1. //
  2. // "$Id: fl_show_colormap.cxx,v 1.5 1999/01/07 19:17:43 mike Exp $"
  3. //
  4. // Colormap color selection dialog for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // Select a color from the colormap.
  27. // Pretty much unchanged from Forms.
  28.  
  29. #include <FL/Fl.H>
  30. #include <FL/Fl_Single_Window.H>
  31. #include <FL/fl_draw.H>
  32. #include <FL/fl_show_colormap.H>
  33. #include <config.h>
  34.  
  35. #define BOXSIZE 14
  36. #define BORDER 4
  37.  
  38. class ColorMenu : public Fl_Window {
  39.   Fl_Color initial;
  40.   Fl_Color which, previous;
  41.   int done;
  42.   void drawbox(Fl_Color);
  43.   void draw();
  44.   int handle(int);
  45. public:
  46.   ColorMenu(Fl_Color oldcol);
  47.   Fl_Color run();
  48. };
  49.  
  50. ColorMenu::ColorMenu(Fl_Color oldcol) :
  51.   Fl_Window(BOXSIZE*8+1+2*BORDER, BOXSIZE*32+1+2*BORDER) {
  52.   clear_border();
  53.   set_modal();
  54.   initial = which = oldcol;
  55. }
  56.  
  57. void ColorMenu::drawbox(Fl_Color c) {
  58.   if (c < 0 || c > 255) return;
  59.   int x = (c%8)*BOXSIZE+BORDER;
  60.   int y = (c/8)*BOXSIZE+BORDER;
  61. #if BORDER_WIDTH < 3
  62.   if (c == which) fl_draw_box(FL_DOWN_BOX, x+1, y+1, BOXSIZE-1, BOXSIZE-1, c);
  63.   else fl_draw_box(FL_BORDER_BOX, x, y, BOXSIZE+1, BOXSIZE+1, c);
  64. #else
  65.   fl_draw_box(c == which ? FL_DOWN_BOX : FL_BORDER_BOX,
  66.           x, y, BOXSIZE+1, BOXSIZE+1, c);
  67. #endif
  68. }
  69.  
  70. void ColorMenu::draw() {
  71.   if (damage() != FL_DAMAGE_CHILD) {
  72.     fl_draw_box(FL_UP_BOX,0,0,w(),h(),color());
  73.     for (int c = 0; c < 256; c++) drawbox((Fl_Color)c);
  74.   } else {
  75.     drawbox(previous);
  76.     drawbox(which);
  77.   }
  78.   previous = which;
  79. }
  80.  
  81. int ColorMenu::handle(int e) {
  82.   int c = which;
  83.   switch (e) {
  84.   case FL_PUSH:
  85.   case FL_DRAG: {
  86.     int X = (Fl::event_x_root() - x() - BORDER);
  87.     if (X >= 0) X = X/BOXSIZE;
  88.     int Y = (Fl::event_y_root() - y() - BORDER);
  89.     if (Y >= 0) Y = Y/BOXSIZE;
  90.     if (X >= 0 && X < 8 && Y >= 0 && Y < 32)
  91.       c = 8*Y + X;
  92.     else
  93.       c = initial;
  94.     } break;
  95.   case FL_RELEASE:
  96.     done = 1;
  97.     return 1;
  98.   case FL_KEYBOARD:
  99.     switch (Fl::event_key()) {
  100.     case FL_Up: if (c > 7) c -= 8; break;
  101.     case FL_Down: if (c < 256-8) c += 8; break;
  102.     case FL_Left: if (c > 0) c--; break;
  103.     case FL_Right: if (c < 255) c++; break;
  104.     case FL_Escape: which = initial; done = 1; return 1;
  105.     case FL_Enter: done = 1; return 1;
  106.     default: return 0;
  107.     }
  108.     break;
  109.   default:
  110.     return 0;
  111.   }
  112.   if (c != which) {
  113.     which = (Fl_Color)c; damage(FL_DAMAGE_CHILD);
  114.     int bx = (c%8)*BOXSIZE+BORDER;
  115.     int by = (c/8)*BOXSIZE+BORDER;
  116.     int px = x();
  117.     int py = y();
  118.     if (px+bx+BOXSIZE+BORDER >= Fl::w()) px = Fl::w()-bx-BOXSIZE-BORDER;
  119.     if (py+by+BOXSIZE+BORDER >= Fl::h()) py = Fl::h()-by-BOXSIZE-BORDER;
  120.     if (px+bx < BORDER) px = BORDER-bx;
  121.     if (py+by < BORDER) py = BORDER-by;
  122.     position(px,py);
  123.   }
  124.   return 1;
  125. }
  126.  
  127. extern char fl_override_redirect; // hack for menus
  128.  
  129. Fl_Color ColorMenu::run() {
  130.   if (which < 0 || which > 255) {
  131.     position(Fl::event_x_root()-w()/2, Fl::event_y_root()-y()/2);
  132.   } else {
  133.     position(Fl::event_x_root()-(initial%8)*BOXSIZE-BOXSIZE/2-BORDER,
  134.          Fl::event_y_root()-(initial/8)*BOXSIZE-BOXSIZE/2-BORDER);
  135.   }
  136.   Fl::grab(*this);
  137.   show();
  138.   done = 0;
  139.   while (!done) Fl::wait();
  140.   Fl::release();
  141.   return which;
  142. }
  143.  
  144. Fl_Color fl_show_colormap(Fl_Color oldcol) {
  145.   ColorMenu m(oldcol);
  146.   return m.run();
  147. }
  148.  
  149. //
  150. // End of "$Id: fl_show_colormap.cxx,v 1.5 1999/01/07 19:17:43 mike Exp $".
  151. //
  152.